package pdm.traj.util.timeslot;

import java.util.Date;

public class HourAndMin {
	public static int MID_NIGHT_MIN_COUNT = 24 * 60;
	private int hour;
	private int min;
	
	public HourAndMin() {
		this.hour = 0;
		this.min = 0;
	}
	
	public HourAndMin(int hour, int min) {
		this.hour = hour;
		this.min = min;
	}
	
	public HourAndMin(String s) {
		String[] ss = s.split(":");
		if (ss.length != 2) {
			hour = 0;
			min = 0;
		} else {
			hour = Integer.parseInt(ss[0]);
			min = Integer.parseInt(ss[1]);
		}
	}
	
	public HourAndMin(Date d) {
		this.hour = d.getHours();
		this.min = d.getMinutes();
	}
	
	public boolean AfterMe(Date d) {
		return toMinCount(d) > this.toMinCount();
	}
	
	public boolean AfterMe(HourAndMin h) {
		return h.toMinCount() > this.toMinCount();
	}
	
	public boolean AfterEqualMe(Date d) {
		return toMinCount(d) >= this.toMinCount();
	}
	
	public boolean AfterEqualMe(HourAndMin h) {
		return h.toMinCount() >= this.toMinCount();
	}
	
	public boolean BeforeMe(Date d) {
		return toMinCount(d) < this.toMinCount();
	}
	
	public boolean BeforeMe(HourAndMin h) {
		return h.toMinCount() < this.toMinCount();
	}
	
	public boolean BeforeEqualMe(Date d) {
		return toMinCount(d) <= this.toMinCount();
	}
	
	public boolean BeforeEqualMe(HourAndMin h) {
		return h.toMinCount() <= this.toMinCount();
	}
	
	private static int toMinCount(Date d) {
		return d.getHours() * 60 + d.getMinutes();
	}
	
	private static int toMinCount(int hour, int min) {
		return hour * 60 + min;
	}
	
	public int toMinCount() {
		return toMinCount(hour, min);
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public int getMin() {
		return min;
	}

	public void setMin(int min) {
		this.min = min;
	}

	public String toString() {
		return String.format("%02d:%02d", hour, min);
	}
	
	public boolean equals(HourAndMin h) {
		return (this.hour == h.hour && this.min == h.min);
	}
	
	public static HourAndMin getFromMinCount(int minC) {
		HourAndMin h = new HourAndMin(minC / 60, minC % 60);
		return h;
	}
	
	public static HourAndMin getMidNight() {
		return new HourAndMin(24, 0);
	}
	
	public static HourAndMin getNext(HourAndMin cur, int step) {
		int nextMC = cur.toMinCount() + step;
		if (nextMC < 0) {
			nextMC += MID_NIGHT_MIN_COUNT;
		}
		if (nextMC >= MID_NIGHT_MIN_COUNT) {
			nextMC -= MID_NIGHT_MIN_COUNT;
		}
		return getFromMinCount(nextMC);
	}
	
//	private static void test() {
//		HourAndMin h1 = new HourAndMin();
//		HourAndMin h2 = new HourAndMin(2, 3);
//		HourAndMin h22 = new HourAndMin(2, 3);
//		HourAndMin h3 = new HourAndMin("03:39");
//		HourAndMin h4 = new HourAndMin(new Date());
//		Date d2 = new Date();
//		d2.setHours(2);
//		d2.setMinutes(3);
//		System.out.println(h1);
//		System.out.println(h2);
//		System.out.println(h3);
//		System.out.println(h4);
//		System.out.println(h1.AfterMe(h2) + "\ttrue");
//		System.out.println(h1.BeforeMe(h2) + "\tfalse");
//		System.out.println(h2.AfterMe(h1) + "\tfalse");
//		System.out.println(h2.BeforeMe(h1) + "\ttrue");
//		System.out.println(h2.BeforeEqualMe(h22) + "\ttrue");
//	}
	
//	public static void main(String[] args) {
//		test();
//	}
	
}
